package com.blogs.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;

import com.blogs.component.GetMoreListView;
import com.blogs.entity.AppConfig;
import com.blogs.entity.Feed;
import com.blogs.entity.MsgEntity;
import com.blogs.service.GetTimeLine.IMsgCallBack;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GetAuthorArticle {
	public interface IMsgCallBack {
		public void onSuccess(ArrayList<Feed> feedList);
		public void onFailed();
	}
	private String url=AppConfig.GET_HOME_PAGE_LINE;
	private GetMsgTask timeLine;
    private IMsgCallBack callback;
	public GetAuthorArticle(int page,String blogapp,IMsgCallBack callback) {
		// TODO Auto-generated constructor stub
		this.callback=callback;
		timeLine=new GetMsgTask(taskCallBack);
		this.url=url.replace("{0}", blogapp).replace("{1}", page+"");
	}
	public void loadData()
	{
		timeLine.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack=new GetMsgTask.onTaskCallBack() {
		public void onSuccess(String result) {
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<ArrayList<Object>> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			ArrayList<Feed> feeds=new ArrayList<Feed>();
			Iterator<Object> iterator = msg.data.iterator();
			while (iterator.hasNext()) 
			{
				Map<String, Object> objs = (Map<String, Object>) iterator.next();
				Feed feed = new Feed(objs);
				feeds.add(feed);
			}
			callback.onSuccess(feeds);
		};
		public void onFailed() {
			callback.onFailed();
		};
	};
}
